package cn.yunhe.action;

import cn.yunhe.eneity.Page;
import cn.yunhe.eneity.User;
import cn.yunhe.service.IUserService;
import cn.yunhe.service.impl.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import lombok.Getter;
import lombok.Setter;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

@Getter
@Setter
public class UserListAction extends ActionSupport implements ModelDriven<User> {
    private User user;
    private int pageSize;
    private int pageNum;
    private Page page;

    @Autowired
    private IUserService userService;

    //503服务器错误启动的时候就发生异常了哦可
    @Override
    public String execute() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("user", user);
        page = userService.queryLikeUsers(map);
        return SUCCESS;
    }
     /*
     添加新用户
     */
    public String addUser() {
        userService.addUser(user);
        return SUCCESS;
    }

    public String delUser(){
        userService.delUser(user);
        return SUCCESS;
    }


    public String getUser(){
       user = userService.getUser(user);
        ServletActionContext.getRequest().setAttribute("data", user);
        return SUCCESS;
    }

    public String updateUser(){
        userService.updateUser(user);
        return SUCCESS;
    }



    @Override
    public User getModel() {
        if (user == null) {
            user = new User();
        }
        return user;
    }
}
